package cn.darkhorse.ticket.basis.service.impl;

import cn.darkhorse.ticket.basis.mapper.TicketUnitInfoMapper;
import cn.darkhorse.ticket.basis.pojo.TicketProduct;
import cn.darkhorse.ticket.basis.pojo.TicketScrapInfo;
import cn.darkhorse.ticket.basis.pojo.TicketUnitInfo;
import cn.darkhorse.ticket.basis.service.TicketProductService;
import cn.darkhorse.ticket.basis.service.TicketScrapInfoService;
import cn.darkhorse.ticket.basis.service.TicketUnitInfoService;
import cn.darkhorse.ticket.code.RepeatCheck;
import cn.darkhorse.ticket.constants.TicketConstants;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 计量单位信息 服务实现类
 * </p>
 *
 * @author maoguanjun
 * @since 2024-01-10
 */
@Service
public class TicketUnitInfoServiceImpl extends ServiceImpl<TicketUnitInfoMapper, TicketUnitInfo> implements TicketUnitInfoService {
    @Resource
    private RepeatCheck<TicketUnitInfo> repeatCheck;
    @Resource
    private TicketScrapInfoService ticketScrapInfoService;
    @Resource
    private TicketProductService ticketProductService;
    /**
     * 列表
     * @param dataName
     * @return
     */
    @Override
    public List<TicketUnitInfo> listAll(String dataName) {
        LambdaQueryWrapper<TicketUnitInfo> wrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dataName)){
            wrapper.like(TicketUnitInfo::getName,dataName);
        }
        wrapper.orderByDesc(TicketUnitInfo::getGmtCreate);
        return list(wrapper);
    }

    /**
     * 保存
     *
     * @param info
     * @return
     */
    @Override
    public boolean saveData(TicketUnitInfo info) {
        //TODO 生成编码
        repeatCheck.repeat(this,info, TicketConstants.BUSINESS_TYPE_DW);
        LambdaQueryWrapper<TicketUnitInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(TicketUnitInfo::getName,info.getName());
        if(null!=info.getId()){
            //校验是否被引用
            if(isQuote(info.getId())){
                throw new ServiceException("该单位已被引用无法修改!");
            }
            wrapper.ne(TicketUnitInfo::getId,info.getId());
        }
        long count = count(wrapper);
        if(count>0){
            throw new ServiceException("该单位名称系统已存在！");
        }
        return saveOrUpdate(info);
    }

    /**
     * 删除
     *
     * @param dataId
     * @return
     */
    @Override
    public boolean delete(Long dataId) {
        //TODO 删除校验
        if(isQuote(dataId)){
          throw new ServiceException("该单位已被引用无法删除!");
        }
        return removeById(dataId);
    }

    /**
     * 查看
     *
     * @param dataId
     * @return
     */
    @Override
    public TicketUnitInfo view(Long dataId) {
        TicketUnitInfo byId = getById(dataId);
        return byId;
    }

    /**
     * 是否被引用
     * @param dataId
     * @return
     */
    public boolean isQuote(Long dataId){
        LambdaQueryWrapper<TicketProduct> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(TicketProduct::getUnity,dataId);
        long count = ticketProductService.count(wrapper);
        LambdaQueryWrapper<TicketScrapInfo> scarpWrapper=new LambdaQueryWrapper<>();
        scarpWrapper.eq(TicketScrapInfo::getUnit,dataId);
        long scarpCount =ticketScrapInfoService.count(scarpWrapper);
        if(count>0||scarpCount>0){
            return true;
        }
        return false;
    }
}
